package logic

import (
	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"
	"cloud-disk/core/models"
	"cloud-disk/core/utils"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileShareSaveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileShareSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileShareSaveLogic {
	return &FileShareSaveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileShareSaveLogic) FileShareSave(req *types.FileShareSaveRequest, UserIdentity string) (resp *types.FileShareSaveResponse, err error) {
	rp := &models.RepositoryPool{}
	err = l.svcCtx.DbConn.Model(&models.RepositoryPool{}).Where("identity=?", req.RepositoryIdentity).First(rp).Error
	if err != nil {
		return
	}

	ur := &models.UserRepository{
		Identity:           utils.GetUUID(),
		Name:               rp.Name,
		UserIdentity:       UserIdentity,
		ParentId:           req.ParentId,
		RepositoryIdentity: req.RepositoryIdentity,
		Ext:                rp.Ext,
	}
	err = l.svcCtx.DbConn.Model(&models.UserRepository{}).Create(ur).Error
	if err != nil {
		return
	}
	return
}
